package com.agentai.framework.common.httpclient;

import cn.hutool.core.text.UnicodeUtil;
import com.agentai.framework.common.constant.Constant;
import lombok.SneakyThrows;

import java.util.HashMap;

/**
 * @author weibaoting
 */
public class RequestUtil {


    /**
     * 第三方接口调用方式
     *
     * @param url       请求地址
     * @param param     请求参数
     * @param way       请求方式 get post
     * @param headerMap 请求头 headerMap
     * @return
     */
    @SneakyThrows
    public static String requestApi(String url, HashMap<String, Object> param, String way, HashMap<String, String> headerMap) {
        HttpClientResult httpClientResult = null;

        try {
            if (Constant.GET.equals(way)) {
                httpClientResult = HttpClientUtils.doGet(url, headerMap, param);
            } else if (Constant.BODY.equals(way)) {
                httpClientResult = HttpClientUtils.doPostJson(url, headerMap, param);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //对返回数据进行解析
        String res = UnicodeUtil.toString(httpClientResult.getContent());
        return res;
    }


}
